#ifndef PSYSPP_H_
#define PSYSPP_H_

#include "xform_node.h"
#include "texture.h"
#include <psys/psys.h>

namespace goatgfx {

class ParticleSystemAttributes {
private:
	Texture *tex;
	psys_attributes *psattr;
	bool own_psattr;

public:
	ParticleSystemAttributes();
	ParticleSystemAttributes(psys_attributes *psattr);
	~ParticleSystemAttributes();

	ParticleSystemAttributes(const ParticleSystemAttributes &rhs);
	ParticleSystemAttributes &operator =(const ParticleSystemAttributes &rhs);

	bool load(const char *fname);
	bool load(FILE *fp);
	bool save(const char *fname) const;
	bool save(FILE *fp) const;

	// particle attributes
	void set_part_color(const Vector3 &color, float t = 0);
	void set_part_alpha(float alpha, float t = 0);
	void set_part_scale(float size, float t = 0);

	// emmiter attributes
	void set_texture(Texture *tex);
	Texture *get_texture() const;

	void set_spawn_range(const Vector3 &range, long tm = 0);

	void set_spawn_rate(float rate, long tm = 0);
	float get_spawn_rate(long tm = 0) const;

	void set_life(float life, float range = 0.0, long tm = 0);
	void set_size(float sz, float range = 0.0, long tm = 0);
	void set_spawn_dir(const Vector3 &dir, const Vector3 &range = Vector3(0, 0, 0), long tm = 0);

	void set_gravity(const Vector3 &grav, long tm = 0);
	void set_drag(float drag);

	void set_particle_limit(int lim);
};

class ParticleSystem : public XFormNode {
private:
	psys_emitter psys;
	ParticleSystemAttributes attr;

	long start_time;	// subtracted from all time calculations
	long last_upd_time;

public:
	ParticleSystem();
	~ParticleSystem();

	void set_start_time(long tm);

	bool is_active() const;

	ParticleSystemAttributes *get_attr();
	const ParticleSystemAttributes *get_attr() const;

	void set_attr(const ParticleSystemAttributes &pattr);

	bool load(const char *fname);
	bool save(const char *fname) const;

	void update(long tm);
	void draw() const;
};

}	// namespace goatgfx

#endif	// PSYSPP_H_
